<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('区域管理')"/>
    <th:block th:include="include :: layout-latest-css"/>
    <th:block th:include="include :: ztree-css"/>
    <th:block th:include="include :: datetimepicker-css"/>
    <th:block th:include="include :: select2-css"/>
    <th:block th:include="include :: bootstrap-select-css"/>
</head>
<style>
    .bootstrap-tree-table .treetable-tbody td {
        white-space: normal !important;
    }

    .select-table {
        overflow: scroll;
        height: 89% !important;
        padding: 10px !important;
    }

    select {
        padding-top: 3px !important;
    }

    .select-list {
        margin-top: 5px !important;
    }

    .treetable-expander {
        width: auto !important;
        height: auto !important;
    }

    .bootstrap-tree-table .treetable-bars .tool-right {
        float: left !important;
        left: 10px;
    }

    .table-striped .table > thead:first-child > tr:first-child > th {
        font-weight: normal;
        font-size: 13px;
        border: 1px solid #cccccc !important;
    }

    .bootstrap-tree-table .treetable-tbody td {
        border: 1px solid #cccccc !important;
        border-top: 0px solid #cccccc !important;
    }

    .select-list li p {
        width: 20px !important;
    }

    .text {
        line-height: 34px;
        margin-bottom: 20px;
        white-space: nowrap;
        text-align: right;
    }

    .col-sm-6_right {
        border-right: 1px solid #cccccc;
    }

</style>
<body class="gray-bg">
<div class="ui-layout-west">
    <div class="box box-main">
        <div class="box-header">
            <div class="box-title">
                <i class="fa icon-grid"></i> 项目名称
            </div>
            <div class="box-tools pull-right">
                <a type="button" class="btn btn-box-tool" href="#" onclick="project()" title="管理项目"><i
                        class="fa fa-edit"></i></a>
                <button type="button" class="btn btn-box-tool" id="btnExpand" title="展开" style="display:none;"><i
                        class="fa fa-chevron-up"></i></button>
                <button type="button" class="btn btn-box-tool" id="btnCollapse" title="折叠"><i
                        class="fa fa-chevron-down"></i></button>
                <button type="button" class="btn btn-box-tool" id="btnRefresh" title="刷新项目"><i
                        class="fa fa-refresh"></i></button>
            </div>
        </div>
        <div class="ui-layout-content">
            <div id="tree" class="ztree"></div>
        </div>
    </div>
</div>
<div class="ui-layout-center">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="region-form">
                    <input type="hidden" id="companyId" name="companyId">
                    <input type="hidden" id="proId" name="proId">
                    <input type="hidden" id="buildingId" name="buildingId">
                    <input type="hidden" id="buildingName" name="buildingName">
                    <input type="hidden" id="proName" name="proName">
                    <div class="select-list">
                        <ul>
                            <li>
                                <p>年：</p>
                                <select id="year" name="year" class="form-control noselect2">
                                    <option value="">请选择</option>
                                    <option th:each="year : ${year}" th:text="${year.name+'年'}"
                                            th:value="${year.name}"></option>
                                </select>
                            </li>
                            <li>
                                <p>月：</p>
                                <select id="month" name="month" class="form-control noselect2">
                                    <option value="">请选择</option>
                                    <option th:each="month : ${month}" th:text="${month.name+'月'}"
                                            th:value="${month.name}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" id="search"><i
                                        class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                        class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                            <li style="float: right;">
                                <a id='submit' class="btn btn-success btn-rounded btn-sm"
                                   onclick="$.modal.openTab('平均单价',ctx +'costSummary/findCurrentUnivalent')"><i
                                        class="fa fa-search"></i>&nbsp;平均单价</a>
                            </li>
                        </ul>
                    </div>
                </form>
                <button id="tex" hidden></button>
                <button id="texEX" hidden></button>
            </div>
            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" id="add"
                   onclick="$.operate.addTab($('#companyId').val()+','+$('#proId').val()+','+$('#buildingId').val())">
                    <i class="fa fa-plus"></i> 新增
                </a>
                <a class="btn btn-info" id="expandAllBtn">
                    <i class="fa fa-exchange"></i> 展开/折叠
                </a>
                <a class="btn btn-info" id="copy" onclick="copy()">
                    <i class="fa fa-files-o"></i> 复制
                </a>
                <a class="btn btn-warning" id="export" onclick="exportCost()">
                    <i class="fa fa-download"></i> 导出成本汇总
                </a>
                <a class="btn btn-warning" id="exportBuilding" onclick="exportData('单体楼')">
                    <i class="fa fa-download"></i> 导出单体楼
                </a>
                <a class="btn btn-info" id="import" onclick="$.table.importExcel()">
                    <i class="fa fa-upload"></i> 导入
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-tree-table"></table>
            </div>
        </div>
    </div>
</div>

<th:block th:include="include :: footer"/>
<th:block th:include="include :: select2-js"/>
<th:block th:include="include :: bootstrap-select-js"/>
<th:block th:include="include :: layout-latest-js"/>
<th:block th:include="include :: ztree-js"/>
<th:block th:include="include :: datetimepicker-js"/>

<script th:inline="javascript">
    var prefix = ctx + "costSummary";
    var building;
    var parentName;
    $(function () {
        var options = {
            code: "codeId",
            parentCode: "parentId",
            uniqueId: "codeId",
            url: prefix + "/list",
            createUrl: prefix + "/add/{id}",
            exportUrl: prefix + "/export",
            importUrl: prefix + "/importData",
            importTemplateUrl: prefix + "/importTemplate",
            modalName: "成本汇总",
            expandAll: false,
            expandFirst: false,
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            columns: [{
                field: 'selectItem',
                radio: true
            },
                {
                    field: 'codeNum',
                    title: '编码编号',
                    width: '100'
                },
                {
                    field: 'codeName',
                    title: '编码名称',
                    width: '100'
                },
                {
                    field: 'codeFeature',
                    title: '特征',
                    width: '100'
                },
                {
                    field: 'codeUnitName',
                    title: '单位',
                    width: '100'
                },
                {
                    field: 'number',
                    title: '目标数量',
                    width: '100'
                },

                {
                    field: 'targetCost',
                    title: '目标成本',
                    width: '100'
                },
                {
                    field: 'changeCost',
                    title: '变动成本',
                    width: '100'
                },
                {
                    field: 'reviseCost',
                    title: '修订成本',
                    width: '100'
                },
                {
                    field: 'changeIncome',
                    title: '变动收入',
                    width: '100'
                },
                {
                    field: 'profitChange',
                    title: '利润变动',
                    width: '100'
                },

                {
                    field: 'inputTax',
                    title: '进项税额',
                    width: '100'
                },
                {
                    field: 'cumulative',
                    title: '截止本期累计',
                    width: '100'
                },
                {
                    field: 'theory',
                    title: '截止本期理论',
                    width: '100'
                },
                {
                    field: 'surplus',
                    title: '截止本期结余',
                    width: '100'
                },
                {
                    field: 'allowance',
                    title: '余量',
                    width: '100'
                }]
        };
        $.treeTable.init(options);
        var panehHidden = false;
        if ($(this).width() < 769) {
            panehHidden = true;
        }
        $('body').layout({initClosed: panehHidden, west__size: 300});
        $('#add').hide();
        $('#copy').hide();
        $('#exportBuilding').hide();
        queryProjectTree();
        $("#search").click(function () {
            if ($("#buildingId").val() != '') {
                if ($("select[name='month']").val() == '' && $("select[name='year']").val() == '') {
                    $.modal.alertWarning("请选择年和月！");
                    return false;
                }
                $("#bootstrap-tree-table").remove();
                var table = '<table id="bootstrap-tree-table"></table>';
                $(".table-striped").append(table);
                if ($("select[name='month']").val() != '') {
                    if (options.columns[16].title == '当期数量') {
                        options.columns.splice(20, 3);
                        options.columns.splice(16, 3);
                        options.columns.splice(11, 1);
                        options.columns.splice(8, 1);
                    }
                    options.columns.splice(8, 0,
                        {
                            field: 'currentChangeCost',
                            title: '当期变动成本',
                            width: '100'
                        })
                    options.columns.splice(11, 0,
                        {
                            field: 'currentChangeIncome',
                            title: '当期变动收入',
                            width: '100'
                        })
                    options.columns.splice(16, 0, {
                            field: 'currentNumber',
                            title: '当期数量',
                            width: '100'
                        },
                        {
                            field: 'currentUnivalent',
                            title: '当期单价',
                            width: '100'
                        }, {
                            field: 'currentTotal',
                            title: '当期总价',
                            width: '100'
                        })
                    options.columns.splice(20, 0, {
                            field: 'currentTheoryNumber',
                            title: '当期理论数量',
                            width: '100'
                        },
                        {
                            field: 'containTaxUnivalent',
                            title: '当期理论单价',
                            width: '100',
                            formatter: function (value, row, index) {
                                if (row.currentTheoryNumber == null) {
                                    value = '';
                                }
                                return value

                            }
                        }, {
                            field: 'currentTheoryTotal',
                            title: '当期理论总价',
                            width: '100'
                        })
                }
            }
            options.url = prefix + '/listMonth?year=' + $("#year").val() + '&month=' + $("#month").val() + '&companyId=' + $("#companyId").val() + '&proId=' + $("#proId").val() + '&buildingId=' + $("#buildingId").val();
            $.treeTable.init(options);
        });

        $("#tex").click(function () {
            $("#year").val("");
            $("#month").val("");
            if (options.columns.length > 16) {
                if (options.columns[16].title == '当期数量') {
                    options.columns.splice(20, 3);
                    options.columns.splice(16, 3);
                }
            }
            if (options.columns[14].title == '税率') {
                options.columns.splice(14, 1);
            }
            if (options.columns[12].title == '税率') {
                options.columns.splice(12, 1);
            }
            if (options.columns[8].title == '当期变动成本') {
                options.columns.splice(11, 1);
                options.columns.splice(8, 1);
            }
            if (options.columns[6].title == '目标单价') {
                options.columns.splice(6, 1);
            }
            $("#bootstrap-tree-table").remove();
            var table = '<table id="bootstrap-tree-table"></table>';
            $(".table-striped").append(table);
            options.url = prefix + '/list?year=' + $("#year").val() + '&month=' + $("#month").val() + '&companyId=' + $("#companyId").val() + '&proId=' + $("#proId").val() + '&buildingId=' + $("#buildingId").val();
            $.treeTable.init(options);
        })

        $("#texEX").click(function () {
            $("#year").val("");
            $("#month").val("");
            if (options.columns.length > 16) {
                if (options.columns[16].title == '当期数量') {
                    options.columns.splice(20, 3);
                    options.columns.splice(16, 3);
                }
            }
            if (options.columns[11].title == '当期变动收入') {
                options.columns.splice(11, 1);
            }
            if (options.columns[8].title == '当期变动成本') {
                options.columns.splice(8, 1);
            }
            if (options.columns[6].title != '目标单价') {
                options.columns.splice(6, 0,
                    {
                        field: 'containTaxUnivalent',
                        title: '目标单价',
                        width: '100'
                    })
                options.columns.splice(12, 0,
                    {
                        field: 'taxRate',
                        title: '税率',
                        width: '100',
                        formatter: function (value, row, index) {
                            if (value == null) {
                                value = ""
                                return value
                            } else {
                                return value + '%'
                            }

                        }
                    })
            }
            $("#bootstrap-tree-table").remove();
            var table = '<table id="bootstrap-tree-table"></table>';
            $(".table-striped").append(table);
            options.url = prefix + '/list?year=' + $("#year").val() + '&month=' + $("#month").val() + '&companyId=' + $("#companyId").val() + '&proId=' + $("#proId").val() + '&buildingId=' + $("#buildingId").val();
            $.treeTable.init(options);
        })
    });

    function exportCost() {
        var fileName = exportFileName();
        $.table.exportData(fileName);
    }

    function exportFileName() {
        var buildingName = $("#buildingName").val();
        var proName = $("#proName").val();
        var fileName = proName + buildingName;
        var year = $("#year").val() == '' ? '' : $("#year").val() + '年';
        var month = $("#month").val() == '' ? '' : $("#month").val() + '月';
        if (buildingName == null || buildingName == '') {
            fileName = "金鹏建设集团";
        }
        return fileName + year + month;
    }

    function exportData(fileName) {
        if ($("select[name='month']").val() != '' && $("select[name='year']").val() == '') {
            $.modal.alertWarning("请选择年！");
            return false;
        }
        fileName = exportFileName();
        $.modal.confirm("确定导出单体楼所有数据吗？", function () {
            $.modal.loading("正在导出数据，请稍后...");
            $.post(prefix + "/exportBuilding", $("#region-form").serializeArray(), function (result) {
                if (result.code == web_status.SUCCESS) {
                    window.location.href = ctx + "common/download?fileName=" + encodeURIComponent(fileName) + ".xls&delete=" + true;
                } else if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg)
                } else {
                    $.modal.alertError(result.msg);
                }
                $.modal.closeLoading();
            });
        });
    }

    function copy() {
        $.modal.open('选择复制的项目', prefix + '/copy?&companyId=' + $("#companyId").val() +
            '&buildingId=' + $("#buildingId").val() + '&proId=' + $("#proId").val() +
            '&proName=' + $("#proName").val() + '&buildingName=' + encodeURIComponent($("#buildingName").val()), '', '550');
    }

    function queryProjectTree() {
        var url = prefix + "/treeData";
        var options = {
            url: url,
            expandLevel: 0,
            onClick: zOnClick
        };
        $.tree.init(options);

        function zOnClick(event, treeId, treeNode) {
            building = treeNode.name;
            parentName = treeNode.parentName;
            if (treeNode.grade == 1) {
                $("#companyId").val(treeNode.id);
                $("#buildingName").val(treeNode.name);
                $("#proName").val(treeNode.parentName);
                $("#proId").val("");
                $("#buildingId").val("");
                $('#add').hide();
                $('#copy').hide();
                $('#exportBuilding').hide();
                $('#export').show();
                $("#tex").click();
            } else if (treeNode.grade == 2) {
                $("#companyId").val(treeNode.pId);
                $("#proId").val(treeNode.id);
                $("#buildingName").val(treeNode.name);
                $("#proName").val(treeNode.parentName);
                $("#buildingId").val("");
                $('#add').hide();
                $('#copy').hide();
                $('#exportBuilding').hide();
                $('#export').show();
                $("#tex").click();
            } else if (treeNode.grade == 3) {
                $.ajax({
                    url: prefix + "/findCompId",
                    async: false,
                    data: {
                        "proId": treeNode.pId
                    },
                    type: "POST",
                    success: function (data) {
                        $("#companyId").val(data);
                        $("#proId").val(treeNode.pId);
                        $("#buildingId").val(treeNode.id);
                        $("#buildingName").val(treeNode.name);
                        $("#proName").val(treeNode.parentName);
                        $('#add').show();
                        $('#copy').show();
                        $('#exportBuilding').show();
                        $('#export').hide();
                        $("#texEX").click();
                    },
                    error: function () {
                    }
                });
            }

        }
    }

    $('#btnExpand').click(function () {
        $._tree.expandAll(true);
        $(this).hide();
        $('#btnCollapse').show();
    });

    $('#btnCollapse').click(function () {
        $._tree.expandAll(false);
        $(this).hide();
        $('#btnExpand').show();
    });

    $('#btnRefresh').click(function () {
        queryProjectTree();
    });

    function project() {
        var url = ctx + "projectManage";
        $.modal.openTab("项目管理", url);
    }

</script>
</body>
<!-- 导入区域 -->
<script id="importTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10">
        <div class="col-xs-offset-1">
            <input type="file" id="file" name="file"/>
            <div class="mt10 pt5">
                <a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i
                        class="fa fa-file-excel-o"></i> 下载模板</a>
            </div>
            <font color="red" class="pull-left mt10">
                提示：仅允许导入“xls”或“xlsx”格式文件！
            </font>
        </div>
    </form>
</script>
</html>